import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;



public class VentanaPrincipal extends JFrame{

	//Constantes
	private static final long serialVersionUID 				= 1L;
	private static final int ANCHO_MINIMO 					= 700;
	private static final int ALTO_MINIMO 					= 550;
	
	//variables
	private ServidorBaseDatos baseDatos 					= new ServidorBaseDatos();
	private PanelSuperior panelSuperior						= new PanelSuperior(baseDatos);
	private PanelIzquierdo panelIzquierdo 					= new PanelIzquierdo(baseDatos,this);
	private JPanel panelDerecho								= new JPanel();

	
	public VentanaPrincipal(int ancho, int alto, String titulo){
		
		super(titulo);
		this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		this.setSize(ancho, alto);
		this.getContentPane().setLayout(new GridBagLayout());
		this.setMinimumSize(new Dimension(ANCHO_MINIMO,ALTO_MINIMO));
		cerrarVentana();
		
		agregarPanelSuperior();
		agregarPanelIzquierdo();
		cambiarPanelDerecho(panelDerecho);
	}
	private void cerrarVentana(){
		
		this.addWindowListener(new WindowListener (){ 	

			@Override
			public void windowClosing(WindowEvent e) {
				if (JOptionPane.showConfirmDialog(null,"Seguro desea salir?", "SISINM", JOptionPane.YES_NO_OPTION)== 0){
					
					try{
						baseDatos.cerrarConexion();
					}catch(NullPointerException e1){
						System.out.println("No se pudo cerrar conexion por: "+e1.getMessage());
					}
					setVisible(false);
					System.exit(0);
				}
				
			} 
			
			@Override
			public void windowActivated(WindowEvent arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void windowClosed(WindowEvent arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void windowDeactivated(WindowEvent arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void windowDeiconified(WindowEvent arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void windowIconified(WindowEvent arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void windowOpened(WindowEvent arg0) {
				// TODO Auto-generated method stub
				
			}

	
		}); 
	}
	
	private void agregarPanelSuperior(){
		
		GridBagConstraints constraints = new GridBagConstraints();
		constraints.gridx = 0;
		constraints.gridy = 0;
		constraints.gridwidth = 3;
		constraints.gridheight = 1;
		constraints.fill = GridBagConstraints.HORIZONTAL;
		constraints.weighty = 0.0; 
		constraints.weightx = 1.0;
		constraints.ipady = 15;
		this.getContentPane().add (panelSuperior, constraints);
	}
	private void agregarPanelIzquierdo(){
		
		GridBagConstraints constraints = new GridBagConstraints();
		constraints.gridx = 0;
		constraints.gridy = 1;
		constraints.gridwidth = 1;
		constraints.gridheight = 2;
		
		constraints.fill = GridBagConstraints.VERTICAL;
		constraints.weighty = 1.0; 
		constraints.weightx = 0.01;
		
		constraints.ipadx = 75;
		
		this.getContentPane().add (panelIzquierdo, constraints); 	
	}
	
	
	public void cambiarPanelDerecho(JPanel panelDerecho){
		
		this.panelDerecho.setVisible(false);
		this.remove(this.panelDerecho);
		
		this.panelDerecho=panelDerecho;
		panelDerecho.setVisible(true);
		
		GridBagConstraints constraints = new GridBagConstraints();
		constraints.gridx = 1;
		constraints.gridy = 1;
		constraints.gridwidth = 2;
		constraints.gridheight = 2;
		constraints.fill = GridBagConstraints.BOTH;
		constraints.weighty = 1.0; 
		constraints.weightx = 1.0; 
		this.getContentPane().add (panelDerecho, constraints);
	}
}
